package fr.esiag.isidis.fi.pdsTeam.presentation;

import java.awt.Adjustable;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;

import fr.esiag.isidis.fi.pdsTeam.generator.EventsGenerator;

public class GeneratorFrame extends JFrame implements ActionListener{
	
	private static final long serialVersionUID = 862960466847268055L;
	static GeneratorFrame instance = null;

    private JTable informationsTable;
    private EventsGenerator generator = new EventsGenerator();
    JScrollBar scrollBarVertical = new JScrollBar();
    JScrollBar scrollbarHorizontal = new JScrollBar(Adjustable.HORIZONTAL);
    
    private JButton boutonGenerate;
    private JButton boutonGenerateInStressMode;
	private JButton boutonPlay;
	private JButton boutonPause;
	private JButton boutonRelancer;
	private JTextField fileName;
	private JTextField ip;
	private JTextField port;
	
	public static GeneratorFrame getInstance() {
		if (instance == null)
			instance = new GeneratorFrame();
		return instance;
	}
	public GeneratorFrame(){
		
	}
	public void launchFrame() throws InterruptedException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(1100, 600);
		this.setTitle("Simulator : Generator component");
		
		scrollBarVertical.setPreferredSize(new Dimension(20, 200));
	    scrollbarHorizontal.setPreferredSize(new Dimension(200, 20));
	    
		this.setLayout(new BorderLayout());
		
		
		JPanel zone1 = new JPanel();
		zone1.setLayout(new FlowLayout(FlowLayout.RIGHT));
		zone1.setPreferredSize(new Dimension(0,40));
		
		getContentPane().add("North",zone1);
		
		informationsTable = new JTable(generator.getInformtions());
		getContentPane().add("Center",new JScrollPane(informationsTable));
		
		JPanel zone2 = new JPanel();
		zone2.setLayout(new FlowLayout(FlowLayout.RIGHT));
		zone2.setPreferredSize(new Dimension(0,40));
		getContentPane().add("South",zone2);
		
		fileName = new JTextField("F:\\scenario.xml", 20);
		ip = new JTextField("localhost", 9);
		port = new JTextField("12345", 5);
		boutonGenerate = new JButton("Start");
		boutonGenerateInStressMode = new JButton("Start in stress mode");
		
		
		boutonPlay = new JButton("Continue");
		boutonPause = new JButton("Pause");
		boutonRelancer = new JButton("Clear & Restart");
		
		
		zone1.add(fileName);
		zone1.add(ip);
		zone1.add(port);
		zone1.add(boutonGenerate);
		zone1.add(boutonGenerateInStressMode);
		
		zone2.add(boutonPlay);
		zone2.add(boutonPause);
		zone2.add(boutonRelancer);
		
		boutonGenerate.addActionListener(this);
		boutonGenerateInStressMode.addActionListener(this);
		boutonPlay.addActionListener(this);
		boutonPause.addActionListener(this);
		boutonRelancer.addActionListener(this);
		 
		informationsTable.add(scrollBarVertical);
		informationsTable.add(scrollbarHorizontal);
		
		setVisible(true);

		/*generator.loadEventsfromXMLFile("f://scenario.xml");
		generator.setPlay(true);
		try {
			generator.startEventSending();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
	
	public void clearTable(){
		generator.setPosition(0);
		generator.getInformtions().clearTable();
		generator.setPlay(true);
	}

	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if(source == boutonGenerate){
			//test l'existance du fichier
			File f = new File(fileName.getText());
			if ( f.exists() ) {
				generator.setPlay(true);
				generator.setStressMode(false);
				generator.loadEventsfromXMLFile(fileName.getText());
				generator.setIpPort(ip.getText(), new Integer(port.getText()));
				generator.start();
			}
			else{
				JOptionPane.showMessageDialog(this,
				    "Le fichier n'existe pas !!!",
				    "GENERATOR ERROR",
				    JOptionPane.ERROR_MESSAGE);
			}
		}else if(source == boutonGenerateInStressMode){
			//test l'existance du fichier
			File f = new File(fileName.getText());
			if ( f.exists() ) {
				generator.setPlay(true);
				generator.setStressMode(true);
				generator.loadEventsfromXMLFile(fileName.getText());
				
				generator.start();
			}
			else{
				JOptionPane.showMessageDialog(this,
				    "Le fichier n'existe pas !!!",
				    "GENERATOR ERROR",
				    JOptionPane.ERROR_MESSAGE);
			}
		} else if(source == boutonPlay){
			generator.setPlay(true);
		}else if(source == boutonPause){
			generator.setPlay(false);
		}else if(source == boutonRelancer){
			clearTable();
		}
	}
}
